/*++++++++++ profile ++++++++++*/
// HashFlow.p4
// Zhao Zongyi, xinshengzzy@foxmail.com, 2020.11.09
/*----------*/
#include "tofino/stateful_alu_blackbox.p4"
#include "tofino/pktgen_headers.p4"
#include <tofino/constants.p4>
#include <tofino/intrinsic_metadata.p4>

#include "includes/headers.p4"
#include "includes/parser.p4"
#include "includes/macro.p4"
#include "includes/registers.p4"
#include "includes/tables.p4"
#include "includes/fieldlists.p4"
#include "includes/metadata.p4"


/*++++++++++ ingress ++++++++++*/
control ingress {
  // stage 0
  apply(forward_t);
  apply(calc_digest_t);
  if (0 == m_meta.promotion) {
    // stage 0
    apply(update_M1_srcip_t);
    apply(update_M1_dstip_t);
    apply(update_M1_proto_t);
    // stage 1
    apply(counter5_t);
    apply(update_M1_cnt_t);
    if (1 == m_meta.M1_collision) {
      // stage 2
      apply(update_M2_srcip_t);
      apply(update_M2_dstip_t);
      apply(update_M2_proto_t);
      // stage 3
      apply(update_M2_cnt_t);
      if (1 == m_meta.M2_collision) {
  	// stage 4
  	apply(update_M3_srcip_t);
  	apply(update_M3_dstip_t);
  	apply(update_M3_proto_t);
  	// stage 5
  	apply(update_M3_cnt_t);
  	if(1 == m_meta.M3_collision) {
	  /* apply(counter1_t); */
	  /* apply(counter2_t); */
  	  // stage 6
	  /* apply(counter3_t); */
  	  apply(compare_M_cnts_t);
  	  apply(update_A_t);
  	  // stage 7
	  /* apply(counter4_t); */
  	  apply(compare_M_A_cnts_t);
  	  // stage 8
	  /* apply(counter5_t); */
  	  apply(resubmit_t);
  	}
      }
    }
    if(1 == m_meta.promotion) {
      apply(counter3_t);
      apply(counter4_t);
    }
  }
  else {
    // stage 0
    apply(rewrite_M1_srcip_t);
    apply(rewrite_M1_dstip_t);
    apply(rewrite_M1_proto_t);
    // stage 1
    apply(rewrite_M1_cnt_t);
    // stage 2
    apply(rewrite_M2_srcip_t);
    apply(rewrite_M2_dstip_t);
    apply(rewrite_M2_proto_t);
    // stage 3
    apply(rewrite_M2_cnt_t);
    // stage 4
    apply(rewrite_M3_srcip_t);
    apply(rewrite_M3_dstip_t);
    apply(rewrite_M3_proto_t);
    // stage 5
    apply(rewrite_M3_cnt_t);
  }
  /* apply(count_ip_packet_t); */
  if(valid(ethernet)) {
    apply(counter1_t);
  }
  apply(counter2_t);
}
/*----------*/

/*++++++++++ egress ++++++++++*/
control egress
{
  // no operation
}
/*----------*/
